/**
 * 
 */
package bplustree;

import java.io.IOException;
import java.util.ListIterator;

import register.Register;

/**
 * @author ernesto
 *
 */
public class RegisterIterator implements ListIterator<Register> {

	private ListIterator<Register> iterator;
	private Leaf presentLeaf;
	private Node superLeaf;
	
	public RegisterIterator (Leaf presentLeaf, ListIterator<Register> iterator){
		this.presentLeaf=presentLeaf;
		this.iterator=iterator;
		superLeaf=new Node(1,presentLeaf.getTree());
	}
	
	/* (non-Javadoc)
	 * @see java.util.ListIterator#add(java.lang.Object)
	 */
	public void add(Register arg0) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see java.util.ListIterator#hasNext()
	 */
	public boolean hasNext() {
		
		return true;
	}

	/* (non-Javadoc)
	 * @see java.util.ListIterator#hasPrevious()
	 */
	public boolean hasPrevious() {
		
		return true;
	}

	/* (non-Javadoc)
	 * @see java.util.ListIterator#next()
	 */
	public Register next(){
		
		if(iterator.hasNext()) return iterator.next();
		//else
		try {
			presentLeaf=(Leaf) superLeaf.loadChild(presentLeaf.getNextLeaf());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		iterator=presentLeaf.registerList.listIterator();
		return this.next();
	}

	/* (non-Javadoc)
	 * @see java.util.ListIterator#nextIndex()
	 */
	public int nextIndex() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see java.util.ListIterator#previous()
	 */
	public Register previous() {
		if(iterator.hasPrevious()) return iterator.previous();
		//else
		try {
			presentLeaf=(Leaf) superLeaf.loadChild(presentLeaf.getPreviousLeaf());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		iterator=presentLeaf.registerList.listIterator(presentLeaf.registerList.size());
		return this.previous();
	}

	/* (non-Javadoc)
	 * @see java.util.ListIterator#previousIndex()
	 */
	public int previousIndex() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see java.util.ListIterator#remove()
	 */
	public void remove() {
		// TODO Auto-generated method stub

	}

	public void set(Register arg0) {
		// TODO Auto-generated method stub
		
	}

}
